/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.gui;

import it.polimi.meteocal.entities.Event;
import it.polimi.meteocal.entities.User;
import it.polimi.meteocal.sessionbeans.EventManager;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.view.ViewScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

@ManagedBean(name = "OtherCalendarBean")
@SessionScoped
public class OtherCalendarBean implements Serializable {
       
    @EJB
    EventManager em;
    
    @ManagedProperty(value="#{SearchUserBean}")
    private SearchUserBean sub;
    
    @ManagedProperty(value="#{InviteUserBean}")
    private InviteUserBean iub;
    
    private ScheduleModel eventModel;
    private ScheduleEvent event = new DefaultScheduleEvent();
    private User ownerUser; 
    private Event myEvent;

    
    //@PostConstruct
    public void init() {
        eventModel = new DefaultScheduleModel();
        ownerUser = sub.getSelectedUser();
        /* Carica tutti gli eventi nel calendario */
        List<Event> organizedEvents = em.loadOrganizedEvents(ownerUser);
        List<Event> participantEvents = em.loadParticipantEvents(ownerUser);
        participantEvents.addAll(organizedEvents);
               
        Iterator<Event> iterator = participantEvents.iterator();
        while(iterator.hasNext()){
            Event e = iterator.next();
            String name = e.getName();
            Date start = e.getStartHour();
            Date end = e.getEndHour();
            eventModel.addEvent(new DefaultScheduleEvent(name, start, end));
        }
    }
    
    public SearchUserBean getSub(){ 
        return sub;
    }
    
    public void setSub(SearchUserBean sub){
        this.sub = sub;
    }
    
    public InviteUserBean getIub(){ 
        return iub;
    }
    
    public void setIub(InviteUserBean iub){
        this.iub = iub;
    }
    
     public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public Event getEvent() {
        if(myEvent == null) {
            myEvent = new Event();
        }  
        return myEvent;
    }
    
    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
        List<Event> organizedEvents = em.loadOrganizedEvents(ownerUser);
        List<Event> participantEvents = em.loadParticipantEvents(ownerUser);
        participantEvents.addAll(organizedEvents);
        
        /* Carico i dati dell'evento selezionato */
        Iterator<Event> iterator = participantEvents.iterator();
        boolean found = false;
        while(iterator.hasNext() && !found){
            Event e = iterator.next();
            String name = e.getName();
            Date start = e.getStartHour(); 
            Date end = e.getEndHour();  
            if(name.equals(event.getTitle()) && start.equals(event.getStartDate()) && end.equals(event.getEndDate())){
                myEvent = e;
                found = true;
            }     
        }
        /* Carico la lista di invitati a quell'evento */
        List<User> invited = myEvent.getInvitedUsers();
        iub.setSelectedUsers(invited);
        
        RequestContext context = RequestContext.getCurrentInstance();
        if(myEvent.getVisibility().equals("public")){
            context.execute("PF('eventDialog').show()");
        }
        else context.execute("PF('privateEventDialog').show()");
    } 

}
